<?php
session_start();
header("Content-type: image/png");
function generaCodice($numCifre) {
	
	if (is_numeric($numCifre))
		$numCifre = (int)$numCifre;
	else
		return "";
	
	$codice = "";
	
	for ($i=0; $i < $numCifre; $i++) {
		
		$num = rand(0,35);
		
		if ($num < 10)
			$codice .= chr($num+48);
		else
			$codice .= chr($num+55);
		
	}
	
	return $codice;
	
}

$x = 150;
$y = 30;
$_SESSION['cod_captcha'] = generaCodice(5);
$code = $_SESSION['cod_captcha'];
$space = $x / (strlen($code)+1);


$img = imagecreatetruecolor($x,$y);

$bg = imagecolorallocate($img,255,255,255);
//$border = imagecolorallocate($img,0,0,0);
$colors[] = imagecolorallocate($img,108,192,64);
$colors[] = imagecolorallocate($img,98,151,188);
$colors[] = imagecolorallocate($img,128,64,192);


imagefilledrectangle($img,0,0,$x,$y,$bg);
//imagerectangle($img,0,0,$x-1,$y-2,$border);


for ($i=0; $i< strlen ($code); $i++)
{
$color = $colors[$i % count($colors)];
imagettftext($img,rand(14,16),rand(-20,20),($i+0.3)*$space,rand(20,25),$color,'font/ERASBD.TTF',$code{$i});
}


for($i=0;$i<100;$i++)
{
$x1 = rand(3,$x-3);
$y1 = rand(3,$y-3);
$x2 = $x1-rand(-5,5);
$y2 = $y1-rand(-5,5);
imageline($img,$x1,$y1,$x2,$y2,$colors[rand(0,count($colors)-1)]);
}

imagepng($img);
?>